﻿Public Class u_PhieuNhap
    Private Sub LoadData()
        Dim pn As New PhieuNhapBUS
        dvgPhieuNhap.DataSource = pn.GetPhieuNhap
        Dim nhom As New NhomBUS
        cbxNhaCC.DataSource = nhom.GetNhomNhaCC
        cbxNhaCC.DisplayMember = "TenNhaCungCap"
        cbxNhaCC.ValueMember = "MaNhaCungCap"
        cbxNhaCC.SelectedIndex = 0
        Dim nv As New NhanVienBUS
        cbxNguoiNhan.DataSource = nv.GetNhanVien
        cbxNguoiNhan.DisplayMember = "TenNV"
        cbxNguoiNhan.ValueMember = "MaNV"
        cbxNguoiNhan.SelectedIndex = 0
    End Sub

    Private Sub u_PhieuNhap_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim newCC As New PhieuNhapDTO
        newCC.MaPhieu = txtMaPhieu.Text
        newCC.NgayXuat = ThoiGian.Value
        newCC.NguoiGiao = txtNguoiGiao.Text
        newCC.NguoiNhan = cbxNguoiNhan.SelectedIndex
        newCC.NguyenLieu = txtNguyenLieu.Text
        newCC.TenNhaCC = cbxNhaCC.SelectedIndex
        newCC.TinhTrang = cbDa.Checked
        newCC.TongTien = txtTongTien.Text
        Dim ccBUS As New PhieuNhapBUS
        ccBUS.AddPhieuNhap(newCC)
        LoadData()
    End Sub


    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim delCC As New PhieuNhapDTO
        delCC.MaPhieu = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "MaPhieu")
        Dim ccBUS As New PhieuNhapBUS
        ccBUS.DelPhieuNhap(delCC)
        LoadData()
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim newCC As New PhieuNhapDTO
        newCC.MaPhieu = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "MaPhieu")
        newCC.NgayXuat = ThoiGian.Value.Date
        newCC.NguoiGiao = txtNguoiGiao.Text
        newCC.NguoiNhan = cbxNguoiNhan.SelectedIndex
        newCC.NguyenLieu = txtNguyenLieu.Text
        newCC.TenNhaCC = cbxNhaCC.SelectedIndex
        newCC.TongTien = txtTongTien.Text
        newCC.TinhTrang = cbDa.Checked
        Dim ccBUS As New PhieuNhapBUS
        ccBUS.UpdatePhieuNhap(newCC)
        LoadData()
    End Sub

    Private Sub dgvPhieuNhap_FocusedRowChanged(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles dgvPhieuNhap.FocusedRowChanged
        txtMaPhieu.Text = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "MaPhieu")
        ThoiGian.Value = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "ThoiGian")
        txtNguoiGiao.Text = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "NguoiGiao")
        cbxNguoiNhan.SelectedValue = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "MaNV")
        txtNguyenLieu.Text = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "NguyenLieu")
        txtTongTien.Text = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "TongTien")
        cbxNhaCC.SelectedValue = dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "TenNhaCungCap")
        If dgvPhieuNhap.GetRowCellValue(dgvPhieuNhap.FocusedRowHandle, "TenNhaCungCap") = True Then
            cbDa.Checked = True
        Else
            cbChua.Checked = True
        End If
    End Sub
End Class
